package com.ebelter.btcomlib.models.bluetooth.products.temperature2;


import android.text.TextUtils;

import com.ebelter.btcomlib.models.bluetooth.products.ear_temperature.EarTemperatureConstant;
import com.ebelter.btcomlib.models.bluetooth.products.ear_temperature.bean.EarTemMesureResult;
import com.ebelter.btcomlib.models.bluetooth.products.temperature2.bean.TemMesureResult2;
import com.ebelter.btcomlib.models.bluetooth.products.temperature2.bean.TemMesureResult2His;
import com.ebelter.btcomlib.models.bluetooth.products.temperature2.interfaces.Temperature2MesureResult;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;


public class Temperature2BytesAnalysis extends BaseHandle {

    private static final String TAG = "Temperature2BytesAnalysis";

    private Temperature2MesureResult temperature2MesureResult;


    public void resultAnalysis(byte[] data) {

        LogUtils.i(TAG, "resultAnalysis----" + BytesUtils.bytes2HexStr(data)+"\n此时connectedBluetoothName = "+connectedBluetoothName);


        if (Temperature2Constant.Temperature2_NAME.equals(connectedBluetoothName)){
            resultAnalysisE_ER(data);
        }else if (EarTemperatureConstant.EarTemperature_NAME.equals(connectedBluetoothName)){
            resultAnalysisER(data);
        }else {
            LogUtils.i(TAG, "resultAnalysis----异常 --异常---异常--解析到的蓝牙名字不匹配 ");
        }






    }
    //解析的是耳温枪
    private void resultAnalysisER(byte[] data) {
        if(data==null||data.length<2){
            return;
        }

        int cmdCode = data[0] & 0xff;
        switch (cmdCode) {
            case 0xff: //接收到当前测量结果
                doResult(data);
                break;
            case 0xee: //错误码
                doError(data);
                break;
            case 0xf0: //关机
                doPoweroff(data);
                break;
        }
    }

    private void doPoweroff(byte[] data) {
        int poweroff = data[1] & 0xff;
        if (poweroff == 0xff) {
            post(new Runnable() {
                @Override
                public void run() {
                    if (temperature2MesureResult != null)
                        temperature2MesureResult.onDevicePoweroff();
                }
            });

        }

    }

    /**
     * 错误码
     */
    private void doError(byte[] data) {
        final int errCmd = data[1] & 0xff;
        LogUtils.i(TAG, "-------doError--------errCmd = " + errCmd);
        post(new Runnable() {
            @Override
            public void run() {
                if (temperature2MesureResult != null) temperature2MesureResult.error(connectedBluetoothName,errCmd);
            }
        });
    }

    /**
     * 收到测试结果
     */
    private void doResult(byte[] data) {
        if (data.length<3){
            return;
        }
        long measureTime = System.currentTimeMillis();
        String mt= TimeUtils.formatTime1(measureTime);
        if (TextUtils.equals(mesurTime,mt)||measureTime-mesurTimeLong<1000) {
            LogUtils.i(TAG,"体温测量时间一致 被reture");
            return;
        }
        mesurTime = mt;
        mesurTimeLong = measureTime;

        int temperat = get2ByteValue(data[2], data[3]);
        float t1 = (float) (temperat * 1.0 / 10.0);

        final TemMesureResult2 mesureResult2 = new TemMesureResult2(mesurTimeLong, t1);
        mesureResult2.com = 0;
        LogUtils.i(TAG, "接收到当前测量结果 " + mesureResult2.toString());
        post(new Runnable() {
            @Override
            public void run() {
                if (temperature2MesureResult != null){
                    temperature2MesureResult.mesureResult(mesureResult2);
                }
            }
        });




    }

    //解析的是额耳温枪
    private void resultAnalysisE_ER(byte[] data){
        if (temperature2MesureResult == null||data == null || data.length != 15) {
            return;
        }
        int cmdCode = data[4] & 0xff;
        switch (cmdCode) {
            case 0xff: //接收到当前测量结果
                analysisCurrentMesureResult(data);
                break;
            case 0xee: //错误码
                mesureError(data);
                break;
            case 0xdd: //信息代码
                infoNotify(data);
                break;
            case 0xaa: //返回最近历史数据
                analysisHisMesureResult(data);
                break;
        }
    }


    //信息回传
    private void infoNotify(final byte[] data) {
        int b5 = (data[5] & 0xff);

        if (b5 == 0xff) {
            post(new Runnable() {
                @Override
                public void run() {
                    if (temperature2MesureResult != null) temperature2MesureResult.setUnitSuccess();
                }
            });
        } else if (b5 == 0xfe) {
            post(new Runnable() {
                @Override
                public void run() {
                    if (temperature2MesureResult != null)
                        temperature2MesureResult.getHisDataFinish();
                }
            });
        } else if (b5 == 0xfd) {
            post(new Runnable() {
                @Override
                public void run() {
                    if (temperature2MesureResult != null)
                        temperature2MesureResult.getVersion(data[6] & 0xff);
                }
            });
        } else if (b5 == 0xfc) {
            post(new Runnable() {
                @Override
                public void run() {
                    if (temperature2MesureResult != null)
                        temperature2MesureResult.onDevicePoweroff();
                }
            });
        }


    }

    private void mesureError(final byte[] data) {
        LogUtils.i(TAG, "---mesureError--- error" + data[5]);
        post(new Runnable() {
            @Override
            public void run() {
                if (temperature2MesureResult != null) temperature2MesureResult.error(connectedBluetoothName,data[5]);
            }
        });
    }


    //解析当前历史数据
    private void analysisHisMesureResult(byte[] data) {

        int year = data[8];
        int month = data[9];
        int day = data[10];
        int hour = data[11];
        int min = data[12];
        int second = data[13];

        LogUtils.i(TAG, "接收到历史测量结果 year =" + year + "   month = " + month + "   day = " + day + "  hour = " + hour + "  min = " + min + "  second = " + second);
        long time = System.currentTimeMillis();

        int temperat = get2ByteValue(data[6], data[7]);
        //3659
        temperat = temperat / 10;
        float t1 = (float) (temperat * 1.0 / 10.0);
        final TemMesureResult2His his = new TemMesureResult2His(time, t1);

        his.com = data[5];
        his.totalNum = data[2];
        his.currentNum = data[3];

        LogUtils.i(TAG, "接收到历史测量结果 " + his.toString());
        post(new Runnable() {
            @Override
            public void run() {
                if (temperature2MesureResult != null) temperature2MesureResult.mesureHisResult(his);
            }
        });


    }

    private String mesurTime;
    private long mesurTimeLong;

    //解析当前结果
    private void analysisCurrentMesureResult(byte[] data) {

        long measureTime = System.currentTimeMillis();
        String mt = TimeUtils.formatTime1(measureTime);
        if (TextUtils.equals(mesurTime, mt) || measureTime - mesurTimeLong < 1000) {
            LogUtils.i(TAG, "体温测量时间一致 被reture");
            return;
        }
        mesurTime = mt;
        mesurTimeLong = measureTime;

        int temperat = get2ByteValue(data[6], data[7]);
        //[02 20 DD 0A FF 03 0E 49 00 00 00 00 00 00 4C]  3657 97.8   97.7
        //3659
        // temperat=temperat/10;
        float t1 = (float) (temperat * 1.0 / 100.0);//Belter_TP有额耳温的

        final TemMesureResult2 mesureResult2 = new TemMesureResult2(mesurTimeLong, t1);
        mesureResult2.com = data[5];
        LogUtils.i(TAG, "接收到当前测量结果 " + mesureResult2.toString());
        post(new Runnable() {
            @Override
            public void run() {
                if (temperature2MesureResult != null){
                    temperature2MesureResult.mesureResult(mesureResult2);
                }
            }
        });

    }


    private int get2ByteValue(byte high, byte low) {
        return ((high & 0xff) << 8) | (low & 0xff);
    }


    void setTemperature2MeasureCallback(Temperature2MesureResult te) {
        this.temperature2MesureResult = te;
    }

    private String connectedBluetoothName;
    public void setConnectedBluetoothName(String name) {
        connectedBluetoothName=name;
        LogUtils.i(TAG,"温度计的蓝牙名字叫 connectedBluetoothName = "+connectedBluetoothName);
    }
}
